﻿//***********************************************************************
// Assembly         : IGolovkine.AoA.Tests
// Author           : Ilia
// Created          : 06-11-2011
//
// Last Modified By : Ilia
// Last Modified On : 01-04-2010
// Description      : 
//
// Copyright        : (c) Microsoft. All rights reserved.
//***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using IGolovkine.AoA.Tcp;

namespace IGolovkine.AoA.Tests
{
    public static class MessageAssert
    {
        private static void AreEqualTunnelConnectionRequest(TcpConnectionInitiated original, TcpConnectionInitiated restored)
        {
            Assert.AreEqual(original.ConnectionId, restored.ConnectionId);
            Assert.AreEqual(original.Host, restored.Host);
            Assert.AreEqual(original.Port, restored.Port);
        }

        private static void AreEqualConnectionCompleted(TcpConnectionTerminated original, TcpConnectionTerminated restored)
        {
            Assert.AreEqual(original.ConnectionId, restored.ConnectionId);
        }

        private static void AreEqualConnectionData(TcpConnectionData original, TcpConnectionData restored)
        {
            Assert.AreEqual(original.ConnectionId, restored.ConnectionId);
            AssertEX.AreEqual(original.GetData(), restored.GetData());
        }

   
        private static void AreEqualTunnelPortRequest(AcceptTcpConnections original, AcceptTcpConnections restored)
        {
            Assert.AreEqual(original.DestinationHost, restored.DestinationHost);
            Assert.AreEqual(original.DestinationPort, restored.DestinationPort);
            Assert.AreEqual(original.SourcePort, restored.SourcePort);
        }
        
        public static void AreEqual(IEnumerable<Message> messages1, IEnumerable<Message> messages2)
        {
            Assert.AreEqual(messages1.Count(), messages2.Count());
            for (int i = 0; i < messages1.Count(); i++)
            {
                AreEqual(messages1.ElementAt(i), messages2.ElementAt(i));
            }
        }

        public static void AreEqual(Message message1, Message message2)
        {
            Assert.IsNotNull(message1);
            Assert.IsNotNull(message2);

            Assert.AreEqual(message1.GetType(), message2.GetType());

            if (message1 is TcpConnectionData)
            {
                AreEqualConnectionData((TcpConnectionData)message1, (TcpConnectionData)message2);
            }
            else if (message1 is TcpConnectionTerminated)
            {
                AreEqualConnectionCompleted((TcpConnectionTerminated)message1, (TcpConnectionTerminated)message2);
            }
            else if (message1 is TcpConnectionInitiated)
            {
                AreEqualTunnelConnectionRequest((TcpConnectionInitiated)message1, (TcpConnectionInitiated)message2);
            }
            else if (message1 is AcceptTcpConnections)
            {
                AreEqualTunnelPortRequest((AcceptTcpConnections)message1, (AcceptTcpConnections)message2);
            }
            else
            {
                Assert.Fail("Unknown message type");
            }
        }
    }
}
